/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.jennic.internal;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.osami.gateway.jennic.IJennicDevice;
import org.osami.gateway.jennic.IPayload;
import org.osami.gateway.jennic.IUpdatableSense;
import org.osami.gateway.jennic.internal.message.EventPayload;
import org.osami.gateway.sensor.SenseNode;
import org.osgi.service.device.Constants;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 16 juin 2010
 * Time: 12:53:46
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
public class JennicDevice implements IJennicDevice {

    public final static int MATCH_SERIAL = 10;
    public final static int MATCH_TYPE = 5;

    public final static String TEMPERATURE_KEY = "Temp";
    public final static String HUMIDITY_KEY = "Hydro";
    public final static String LIGHT_KEY = "Light";

    @ServiceProperty(name = Constants.DEVICE_CATEGORY,
                     value = "{Jennic}")
    private String[] categories;

    @ServiceProperty(name = Constants.DEVICE_SERIAL,
                     mandatory = true)
    private String serial;

    @ServiceProperty(name = "device.type",
                     value = "jennic")
    private String type;

    @ServiceProperty(value = SenseNode.TOPIC_PREFIX + "jennic")
    private String topic;

    @Requires(id = "temperature",
              filter = "(&(sense.type=Temp)(device.id=<to-be-set-dynamically>))",
              optional = true)
    private IUpdatableSense temperature;

    @Requires(id = "humidity",
              filter = "(&(sense.type=Hydro)(device.id=<to-be-set-dynamically>))",
              optional = true)
    private IUpdatableSense humidity;

    @Requires(id = "light",
              filter = "(&(sense.type=Light)(device.id=<to-be-set-dynamically>))",
              optional = true)
    private IUpdatableSense light;

    public void noDriverFound() {

    }

    public void dispatch(IPayload payload) {
        // We will only handle event type message
        if (IPayload.Type.EVENT.equals(payload.getType())) {
            EventPayload event = (EventPayload) payload;

            String value = event.get(TEMPERATURE_KEY);
            if (value != null) {
                System.out.println("Event contains temperature data");
                temperature.update(Integer.parseInt(value));
            }
            value = event.get(LIGHT_KEY);
            if (value != null) {
                System.out.println("Event contains light data");
                light.update(Integer.parseInt(value));
            }
            value = event.get(HUMIDITY_KEY);
            if (value != null) {
                System.out.println("Event contains humidity data");
                humidity.update(Integer.parseInt(value));
            }
        }
    }
}
